﻿using System;
using System.Text;
using RabbitMQ.Client;

namespace Send
{
    class Send
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() {HostName = "localhost"};
            using var connection=factory.CreateConnection();
            using var channel=connection.CreateModel();
            //声明队列，通过指定 durable 参数为true，对消息进行持久化处理
            //这样即使RabbitMQ服务器重启，task_queue队列也不会丢失
            channel.QueueDeclare("task_queue", true, false, false, null);

            string message = GetMessage(args);//"Hello World"

            var body = Encoding.UTF8.GetBytes(message);

            //将消息标记为持久性
            var properties = channel.CreateBasicProperties();
            properties.Persistent = true;

            channel.BasicPublish(exchange:"", routingKey:"task_queue",basicProperties:properties,body:body);
            Console.WriteLine("[x] Send {0}",message);

            Console.WriteLine("Press [enter] to exit.");
            Console.ReadLine();
        }

        private static string GetMessage(string[] args)
        {
            return ((args.Length)>0?string.Join(" ",args):"Hello World");
        }
    }
}
